package com.bing.services.impl;

import com.bing.services.UtilService;
import com.bing.utils.DBUtil;
import com.bing.utils.UtilFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Paths;

/**
 * Created by kaibb on 16-1-14.
 */
@Component
public class UtilServiceImpl implements UtilService ,InitializingBean{
    @Value("${dataSource.dbName:''}")
    private String dbName;

    @Value("${dataSource.userName:''}")
    private String userName;

    @Value("${dataSource.passWord:''}")
    private String password;

    @Value("${env.db.backUp:backUp:''}")
    private String backUpPath;


    @Override
    public void createAndGetFileUrl(File parent) {

    }

    @Override
    public boolean backUpDB() {
        String backUpName = Paths.get(backUpPath,UtilFactory.getTimeStr()).toFile().getAbsolutePath();
        return DBUtil.backUp(userName,password, dbName,backUpName);
    }

    @Override
    public Boolean restoreDB(String date) {
        String restoreName = Paths.get(backUpPath,date).toFile().getAbsolutePath();
        return DBUtil.restore(userName,password,dbName,restoreName);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        UtilFactory.createDir(backUpPath);
    }
}
